function [F_test, Autocorr_test, White_test]  = zFC_Ftest_Autocorr_Homosk(yyy,xxx,xxx_rest)

%==========================================================================
% In the regression of yyy on xxx, computes the F test on the null
% hypothesis that the coefficients on the variables in xxx not included in
% xxx_rest are jointly equal to zero. The test holds under the maintained
% hypothesis that the error in the regression of yyy on xxx satisfy the
% A4GM, i.e. no autocorrelation nor heteroskedasticity. The code runs the
% Box Ljiung test of autocorrelations of the residuals to 4 lags and 8 lags
% and the White test on the residuals

% The results of the tests are reported with the statistic and the
% corresponding pvalue

% All statistics checked on Stata


%==========================================================================


%===== F TEST on the coefficients 

% restricted model
coeff_rest = (xxx_rest'*xxx_rest)^(-1)*xxx_rest'*yyy;
resid_rest = yyy - xxx_rest*coeff_rest;
RSS_rest = resid_rest'*resid_rest;

% unrestricted model
coeff = (xxx'*xxx)^(-1)*xxx'*yyy;
resid = yyy - xxx*coeff;
RSS = resid'*resid;

% Compute statistic
F_stat = (RSS_rest-RSS)*(size(yyy,1)-size(xxx,2))/(RSS*(size(xxx,2)-size(xxx_rest,2)));
F_pvalue = 1-fcdf(F_stat,size(xxx,2)-size(xxx_rest,2),size(yyy,1)-size(xxx,2));
F_test = [F_stat, F_pvalue]; % (Same as Wald statistic computed as W/r)

 
%===== Test of autocorrelations in the residuals
Autocorr_test = NaN*ones(2,2);
[stat, pvalue] = zFC_LjungBox(resid,4);
Autocorr_test(1,:) = [stat, pvalue];
[stat, pvalue]= zFC_LjungBox(resid,8);
Autocorr_test(2,:) = [stat, pvalue];


% %===== White test on homoskedasticity of the residuals
% fittedv = xxx*coeff;
% yyy_Whitetest = resid.^2;
% N = length(yyy);
% xxx_Whitetest = [ones(N,1), fittedv, fittedv.^2];
% 
% % restricted
% coeff_Whitetest = (ones(N,1)'*ones(N,1))^(-1)*ones(N,1)'*yyy_Whitetest;
% resid_Whitetest = yyy_Whitetest - ones(N,1)*coeff_Whitetest;
% RSSrest_Whitetest = resid_Whitetest'*resid_Whitetest;
% 
% % unrestricted
% coeff_Whitetest = (xxx_Whitetest'*xxx_Whitetest)^(-1)*xxx_Whitetest'*yyy_Whitetest;
% resid_Whitetest = yyy_Whitetest - xxx_Whitetest*coeff_Whitetest;
% RSS_Whitetest = resid_Whitetest'*resid_Whitetest;
% 
% White_stat = (RSSrest_Whitetest-RSS_Whitetest)*(size(yyy_Whitetest,1)-size(xxx_Whitetest,2))/(RSS_Whitetest*(2));
% White_pvalue = 1-fcdf(White_stat,2,size(yyy_Whitetest,1)-size(xxx_Whitetest,2));
% White_test = [White_stat, White_pvalue];
White_test = [NaN, NaN];

end

